package com.hundun.hddeal.service.lmpl;

import com.hundun.hddeal.dao.AdUserMapper;
import com.hundun.hddeal.dao.MtUserMapper;
import com.hundun.hddeal.entity.Gzh;
import com.hundun.hddeal.entity.MtUser;
import com.hundun.hddeal.entity.User;
import com.hundun.hddeal.response.Result;
import com.hundun.hddeal.response.ResultUtil;
import com.hundun.hddeal.service.MtUserService;
import com.hundun.hddeal.service.TokenService;
import com.hundun.hddeal.util.MD5Util;
import com.hundun.hddeal.util.RedisUtil;
import com.hundun.hddeal.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 媒体用户业务层
 * @author 谢
 */
@Service("MtUserService")
public class MtUserServiceImpl implements MtUserService {


    @Autowired
    MtUserMapper mtUserMapper;
    @Autowired
    GzhServiceImpl gzhService;
    @Autowired
    TokenService tokenService;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    AdUserMapper adUserMapper;

    @Override
    public Result<MtUser> register(MtUser record) {
        try {
            MtUser mtUser = mtUserMapper.selectByMtUserId(record.getMtId());
            if (mtUser != null){
                return ResultUtil.error("已存在");
            }
            MtUser mtUser1 =mtUserMapper.selectPhone(record.getMtPhone());
            if(mtUser1!=null){
                return ResultUtil.error("该手机号码已被注册");
            }

            String noteCode=(String)redisUtil.get(record.getMtPhone());
            if(noteCode==null ){
                return ResultUtil.error("验证码已失效");
            }else if(!record.getNoteCode().equals(noteCode)){
                return ResultUtil.error("验证码错误");
            }

            if(record.getUserCode()!=null){
                User user;
                user = adUserMapper.selectUserCode(record.getUserCode());
                if(user==null){
                    return ResultUtil.error("请填入正确的邀请码！");
                }else if(!("3".equals(user.getFz()))){
                    return ResultUtil.error("该邀请码不是正确的！");
                }
                record.setUserName(user.getUsername());
                record.setFz(user.getFz());
            }

            String takePwd=record.getMtPwd();
            record.setTakePwd(MD5Util.getSaltMD5(takePwd));
            record.setMtPwd(MD5Util.getSaltMD5(record.getMtPwd()));
            mtUserMapper.insertSelective(record);
            return ResultUtil.success(mtUser);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    @Override
    public MtUser selectByMtUserId(String mtId) {
        return mtUserMapper.selectByMtUserId(mtId);
    }

    /**
     * 主键查找用户
     * @param id
     * @return
     */
    @Override
    public MtUser selectById(Long id) {
        try {

            MtUser mtUser = mtUserMapper.selectByPrimaryKey(id);
            if (mtUser!=null){
                return mtUser;
            }
            return null;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


    @Override
    public Result findMtUserByMtId(String mtId) {
        try {
            MtUser mtuser = mtUserMapper.findMtUserByMtId(mtId);
            if (mtuser == null) {
                return ResultUtil.error("不存在该用户");
            }
            List<Gzh> gzhList = gzhService.selectMtId(mtuser.getId());
            if (gzhList != null) {
                return ResultUtil.ISOK(mtuser);
            }else {
                mtuser.setMtPwd(null);
                mtuser.setTakePwd(null);
                return ResultUtil.ISNULL(mtuser);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.error();
    }


    /**
     * 通过传入用户查找用户并保存token
     *
     * @param record
     * @return
     */
    @Override
    public Result selectByMtUserId(MtUser record) {
        try {
            MtUser mtUser;
            if(record.getType()==1){
                if (TextUtils.isEmpty(record.getMtId())) {
                    return ResultUtil.error("请输入用户名");
                }
                if (TextUtils.isEmpty(record.getMtPwd())) {
                    return ResultUtil.error("请输入密码");
                }
               mtUser = mtUserMapper.selectByMtUserId(record.getMtId());
            }else {
                if(TextUtils.isEmpty(record.getMtPhone())){
                    return ResultUtil.error("手机号为空");
                }
               mtUser = mtUserMapper.selectPhone(record.getMtPhone());
            }
            if (mtUser == null) {
                return ResultUtil.error("不存在该用户");
            }
            if (!MD5Util.getSaltverifyMD5(record.getMtPwd(),mtUser.getMtPwd())) {
                return ResultUtil.error("密码输入错误");
            }
            String token = tokenService.getToken(mtUser.getMtId());
            mtUser.setTakePwd(null);
            mtUser.setMtPwd(null);
            Map<String, Object> map = new HashMap<>(4);
            map.put("mtUser", mtUser);
            map.put("mtUserToken", token);
            return ResultUtil.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }


    /**
     * 更新用户
     * @param record
     * @return
     */
    @Override
    public Result updateByPrimaryKeySelective(MtUser record) {
        try{
            mtUserMapper.updateByPrimaryKeySelective(record);
            record.setMtPwd(null);
            record.setTakePwd(null);
            return ResultUtil.success(record);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    @Override
    public Result noteLogin(String mtPhone, String note) {
        try{
            if(mtPhone==null){
                return ResultUtil.error("手机号为空!");
            }
            MtUser mtUser;
            if(note==null){
                return ResultUtil.error("验证码为空!");
            }
            mtUser = mtUserMapper.selectPhone(mtPhone);
            String rnote=(String)redisUtil.get(mtPhone);
            if(rnote==null){
                return ResultUtil.error("验证码已过期!");
            }else if(!note.equals(rnote)){
                return ResultUtil.error("验证码不正确!");
            }else{
                redisUtil.del(mtPhone);
            }
            String token = tokenService.getToken(mtUser.getMtId());
            mtUser.setTakePwd(null);
            mtUser.setMtPwd(null);
            Map<String, Object> map = new HashMap<>(2);
            map.put("mtUser", mtUser);
            map.put("mtUserToken", token);
            return ResultUtil.success(map);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error();
        }
    }


}
